package com.cxb;

import java.util.ArrayList;
import java.util.List;

import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.RemoteException;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.amitek.aidl.mqtt.MqttServiceProxy;
import com.amitek.aidl.mqtt.bean.StatInfo;
import com.cxb.adapter.SettingAdapter;
import com.cxb.entity.SettingButtonInfo;
import com.cxb.service.CXBMainService;
import com.cxb.utils.Config;
import com.cxb.utils.SharedPreferencesUtils;

/**
 * @ClassName: SettingActivity
 * @Description: 设置界面，滑动切�?
 * @author 杨瑜 45154139@qq.com
 * @date 2013-9-25 上午10:20:05
 * 
 */
public class SettingActivity extends BaseActivity implements
		OnItemClickListener {
	private ViewPager vpSetting;// 页卡内容
	private View vSetting1, vSetting2, vSetting3;
	private Button ibBack;
	private List<View> listViews; // Tab页面列表
	private GridView gvSetting1, gvSetting2, gvSetting3;
	private List<SettingButtonInfo> list;
	private SettingAdapter saSetting1, saSetting2, saSetting3;
	private String[] sSettingButtonName;
	private Resources res;
	private Intent[] intents;
	private LinearLayout llyPagePoint;
	ArrayList<ImageView> al = new ArrayList<ImageView>();
	private List<SettingButtonInfo> settingButtonList = new ArrayList<SettingButtonInfo>();

	Intent sosIntent;
	private SharedPreferencesUtils spPreferencesUtils;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		res = getResources();
		spPreferencesUtils = new SharedPreferencesUtils(this);
		sosIntent = new Intent("android.intent.action.CALL");

		// //////////////////////////////////////////////////////////////////////////////

		if (Config.isRecognizeSupport(this)) {
			Intent nIntent = new Intent();
			ComponentName cn = new ComponentName("com.unisound.unicar.gui",
					"com.unisound.unicar.gui.ui.GUIMainActivity");
			nIntent.setComponent(cn);
			settingButtonList.add(new SettingButtonInfo(
					getString(R.string.setting_soundcontrol),
					R.drawable.icon_yuzhisheng, nIntent, StatInfo.WIFI));
		}
		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_wifi), R.drawable.icon_setting_wifi,
				new Intent(SettingActivity.this, WifiActivity.class),
				StatInfo.WIFI));
		// settingButtonList
		// .add(new SettingButtonInfo(getString(R.string.setting_borr),
		// R.drawable.icon_setting_rada, new Intent(
		// SettingActivity.this,
		// BlueToothManagerActivity.class), StatInfo.INFO));
		if (Config.isEcardSupport(this)) {
			settingButtonList.add(new SettingButtonInfo(
					getString(R.string.wifi_ap), R.drawable.icon_hotspot,
					new Intent(SettingActivity.this, WifiAPActivity.class),
					StatInfo.INFO));
		}

//		Intent electronicEyeintent = new Intent("android.intent.action.MAIN");
//		electronicEyeintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		electronicEyeintent.setClassName("com.autonavi.minimap",
//				"com.autonavi.map.activity.NewMapActivity");
//		electronicEyeintent
//				.setData(Uri
//						.parse("androidamap://openFeature?featureName=Mine&page=ToolBox&item=ElectronicEye&clearStack=1"));
//
//		settingButtonList.add(new SettingButtonInfo(
//				getString(R.string.setting_edog), R.drawable.btn_edog,
//				electronicEyeintent, StatInfo.NAVIGATE));

		// settingButtonList.add(new SettingButtonInfo(
		// getString(R.string.setting_edog_alarm), R.drawable.edog,
		// new Intent(SettingActivity.this, EdogActivity.class),
		// StatisticsManager.BTN_BRIGHT));
		// settingButtonList.add(new SettingButtonInfo(
		// getString(R.string.setting_borr), R.drawable.icon_setting_rada,
		// new Intent(SettingActivity.this, GensorActivity.class),
		// StatisticsManager.BTN_LAND));

		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_wexin),
				R.drawable.icon_setting_weixin, new Intent(
						SettingActivity.this, WeiXinBindActivity.class),
				StatInfo.WEIXIN));
		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_navhis),
				R.drawable.icon_setting_navhistroy, new Intent(
						SettingActivity.this, HistoryListActivity.class),
				StatInfo.LEVEL));
		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_device_info),
				R.drawable.icon_setting_systeminfo, new Intent(
						SettingActivity.this, DeviceInfoActivityK.class),
				StatInfo.INFO));
		if (!Config.isEcardSupport(this)) {
			settingButtonList.add(new SettingButtonInfo(
					getString(R.string.setting_pay),
					R.drawable.icon_setting_pay, new Intent(
							SettingActivity.this, RechangeIOPActivity.class),
					StatInfo.CHARGE));

		}
		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_brigtness),
				R.drawable.icon_setting_brightness, new Intent(
						SettingActivity.this, BrightnessActivity.class),
				StatInfo.BRIGHT));
		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_upgrade),
				R.drawable.icon_setting_system_upgrade, new Intent(
						SettingActivity.this, SystemUpdateActivity.class),
				StatInfo.UPGRADE));

		settingButtonList.add(new SettingButtonInfo(
				getString(R.string.setting_shutdown),
				R.drawable.icon_setting_prower, new Intent(
						SettingActivity.this, PowerOffActivity.class),
				StatInfo.SHUTDOWN));

		// Intent reboot_Intent = new Intent(SettingActivity.this,
		// PowerOffActivity.class);
		// reboot_Intent.putExtra("reboot", 1);
		//
		// settingButtonList.add(new SettingButtonInfo(
		// getString(R.string.setting_restart), R.drawable.setting_restart,
		// reboot_Intent,
		// StatisticsManager.BTN_DEVICE_INFO,new
		// SettingButtonInfo.ItemOnClickListener() {
		//
		// @Override
		// public void onItemClick(View view, int index) {
		// // TODO Auto-generated method stub
		// AlertDialog.Builder builder = new Builder(
		// SettingActivity.this);
		// builder.setTitle("是否确定要重启");
		// builder.setPositiveButton(R.string.confirm,
		// new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// RootContext.getInstance().runCommand("reboot");
		// }
		//
		// });
		// builder.setNegativeButton(R.string.cancel,
		// new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// });
		// builder.create().show();
		// }
		// }));

		// //////////////////////////////////////////////////////////////////////////////

		InitViewPager();
		InitSetting1();
		InitSetting2();
		// InitSetting3();

	}

	@Override
	protected void onStart() {
		sosIntent
				.setData(Uri.parse("tel:" + spPreferencesUtils.getSOSNumber()));
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	private void InitSetting1() {
		gvSetting1 = (GridView) vSetting1.findViewById(R.id.gv_setting1);
		list = new ArrayList<SettingButtonInfo>();
		for (int i = 0; i < 6 && i < settingButtonList.size(); i++) {
			list.add(settingButtonList.get(i));
		}
		saSetting1 = new SettingAdapter(this);
		saSetting1.setList(list);
		gvSetting1.setAdapter(saSetting1);
		gvSetting1.setOnItemClickListener(this);
	}

	private void InitSetting2() {
		gvSetting2 = (GridView) vSetting2.findViewById(R.id.gv_setting2);
		list = new ArrayList<SettingButtonInfo>();
		for (int i = 6; i < settingButtonList.size() && i < 12; i++) {
			list.add(settingButtonList.get(i));
		}
		saSetting2 = new SettingAdapter(this);
		saSetting2.setList(list);
		gvSetting2.setAdapter(saSetting2);
		gvSetting2.setOnItemClickListener(this);
	}

	private void InitSetting3() {

		gvSetting3 = (GridView) vSetting3.findViewById(R.id.gv_setting2);
		list = new ArrayList<SettingButtonInfo>();
		for (int i = 12; i < settingButtonList.size(); i++) {
			list.add(settingButtonList.get(i));
		}
		saSetting3 = new SettingAdapter(this);
		saSetting3.setList(list);
		gvSetting3.setAdapter(saSetting3);
		gvSetting3.setOnItemClickListener(this);
	}

	/**
	 * @Title: InitViewPager
	 * @Description: 初始化滑动切换效�?
	 * @date 2013-9-30 上午11:07:12
	 * @author 陈伟斌
	 */
	private void InitViewPager() {
		llyPagePoint = (LinearLayout) findViewById(R.id.lly_pagepoint);
		for (int i = 0; i < 2; i++) {
			ImageView iv = new ImageView(this);
			LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			layout.setMargins(4, 0, 4, 0);
			iv.setLayoutParams(layout);

			if (i == 0) {
				iv.setBackgroundResource(R.drawable.point_selected);
			} else {
				iv.setBackgroundResource(R.drawable.point_nor);
			}
			al.add(iv);
			llyPagePoint.addView(iv);
		}

		llyPagePoint.setVisibility(View.VISIBLE);// 只有一页不显示
		vpSetting = (ViewPager) findViewById(R.id.vp_setting);
		LayoutInflater mInflater = getLayoutInflater();
		vSetting1 = mInflater.inflate(R.layout.activity_setting1, null);
		vSetting2 = mInflater.inflate(R.layout.activity_setting2, null);
		vSetting3 = mInflater.inflate(R.layout.activity_setting2, null);
		listViews = new ArrayList<View>();
		listViews.add(vSetting1);
		listViews.add(vSetting2);
		// listViews.add(vSetting3);
		vpSetting.setAdapter(new MyPagerAdapter(listViews));
		vpSetting.setCurrentItem(0);
		vpSetting.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * @ClassName: MyPagerAdapter
	 * @Description: ViewPager适配�?
	 * @author 杨瑜 45154139@qq.com
	 * @date 2013-9-25 上午10:21:06
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * @ClassName: MyOnPageChangeListener
	 * @Description: 页卡切换监听
	 * @author 杨瑜 45154139@qq.com
	 * @date 2013-9-25 上午10:21:31
	 * 
	 */

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			// pageIndex=arg0;
			for (int i = 0; i < al.size(); i++) {
				if (i == arg0) {
					al.get(i).setBackgroundResource(R.drawable.point_selected);
				} else {
					al.get(i).setBackgroundResource(R.drawable.point_nor);
				}
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	int count = 0;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		SettingButtonInfo info = (SettingButtonInfo) arg0.getAdapter().getItem(
				arg2);
		if (info.getItemOnClickListener() != null) {
			info.getItemOnClickListener().onItemClick(arg1, arg2);
			return;
		}
		if (info.getIntent() != null) {
			startActivity(info.getIntent());
			count = 0;
		}
		MqttServiceProxy mqttProxy = CXBMainService.getMqttProxy();
		if (mqttProxy != null) {
			try {
				mqttProxy.eventStatistics(info.getEventName());
			} catch (RemoteException e) {
				e.printStackTrace();
			}// 行程结束
		}
	}
}